<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.tabDati.php');


$sSql = "SELECT mg.* "
	." FROM rgl_mod_rglob mg "
	." INNER JOIN rgl_mod_rbil mr ON mr.id_mod_rbil = mg.id_mod_rbil "
	." INNER JOIN rgl_bilancio b ON b.id_bil = ".reqVarInt('id')
	."    AND b.id_set_dati = mr.id_set_dati ";
$objDB->query($sSql);
$aSelMRG = array();
while($aX = $objDB->fetch()) {
	$aSelMRG[$aX['id_mod_rglob']] = $aX['cod_mod_rglob']." - ".$aX['descr'];
	reqDef('id_mod_rglob',$aX['id_mod_rglob']);
}

$sSql = "SELECT mr.*, mg.id_mod_rglob, mg.id_mod_rbil "
	." FROM rgl_mod_rglob mg "
	." INNER JOIN rgl_mod_rbil mr ON mr.id_mod_rbil = mg.id_mod_rbil "
	." WHERE id_mod_rglob = ".reqVarInt('id_mod_rglob');
$objDB->query($sSql);

$aVar = array('B_COD_SETT'=>'***');
$aDefDati = array();
if ($acMRG = $objDB->fetch()) {
	$msgPag = aVarCalcTabDati(reqVarInt('id'), reqVarInt('idp')
		,$aVar, $aDefDati, 99);
	$dataRif = $aVar['B_DATA_RIF'];
	$idSetDati = $aVar['B_ID_SET_DATI'];
}

$sqlSel = "SELECT * "
	." FROM rgl_settore ORDER BY cod_sett";
$objDB->query($sqlSel,'selRecSett');
$aSett = array('***'=>'*** settore generico');
while($aX = $objDB->fetch('selRecSett')) {
	$aSett[$aX['cod_sett']] = $aX['cod_sett']." - ".$aX['descr'];
}
reqDef('cod_sett',$aVar['B_COD_SETT']);

$sCmdL = '';
if ($acMRG) {
	$sCmdL .= "<a class='cmd1' href='indiciPdf.php?id=".reqVarInt('id')."&idp=".reqVarInt('idp')
		."&ttd=".rglVx($acMRG,'tipo_tab_indici')."&se=".urlencode(reqVar('cod_sett'))
		."&sed=".urlencode($aSett[reqVar('cod_sett')])."&idm=".rglVx($acMRG,'id_mod_rbil')
		."&xsn=".XSN."'>PDF</a>";
}
$sCmdR = '';
$sCmdR .= "<a class='cmd1' href='bilCeeVis.php?id=".reqVarInt('id')."&idp=".reqVarInt('idp')
	."&xsn=".XSN."'>bilancio</a>";
$sCmdR .= "&nbsp;&nbsp;&nbsp;&nbsp;";
$sCmdR .= "<a class='cmd1' href='bilRicVis.php?id=".reqVarInt('id')."&idp=".reqVarInt('idp')
	."&xsn=".XSN."'>bilancio riclassificato</a>";
if (reqVarInt('idp')>0) {
	$sCmdR .= "&nbsp;&nbsp;&nbsp;&nbsp;";
	$sCmdR .= "<a class='cmd1' href='renFinVis.php?id=".reqVarInt('id')."&idp=".reqVarInt('idp')
		."&xsn=".XSN."'>rendiconto finanziario</a>";
}
$sCmdR .= cmdTorna('torna');


$aOpzTesta['aMenuVar']['bilancio CEE']
	['href'] = RGL_URL_ROOT."/app/ratg/cbil/bilCeeVis.php?id=".reqVarInt('id')."&idp=".reqVarInt('idp');
$aOpzTesta['aMenuVar']['bilancio riclassificato']
	['href'] = RGL_URL_ROOT."/app/ratg/cbil/bilRicVis.php?id=".reqVarInt('id')."&idp=".reqVarInt('idp');
$aOpzTesta['aMenuVar']['rendiconto finanziario']
	['href'] = RGL_URL_ROOT."/app/ratg/cbil/renFinVis.php?id=".reqVarInt('id')."&idp=".reqVarInt('idp');
$aOpzTesta['aMenuVar']['indici']
	['href'] = RGL_URL_ROOT."/app/ratg/cbil/indiciVis.php?id=".reqVarInt('id')."&idp=".reqVarInt('idp');

testaHtml('', $msgPag, 'ratg',$aOpzTesta);
?>
<script type="text/javascript">
<!--
function submAz(az) {
	document.frm1.azione.value = az;
	document.frm1.submit();
}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo getHtmlForm('frm1','',array('enctype'=>true));
echo getHtmlHidden('azione','...');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('id');
echo getHtmlHidden('idp');
?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" align="center" colspan='20'><b>Azienda</b></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Cod.fisc.</td>
      <td class="txt1" align="left" colspan='7'><?=$aVar['B_COD_FISC']?></td>
      <td class="lab1" align="left" colspan='3'>Cod CCIAA</td>
      <td class="txt1" align="left" colspan='7'><?=$aVar['B_COD_CCIAA']?></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Ragione sociale</td>
      <td class="txt1" align="left" colspan='17'>
      	<a class='txt1' href='anagVis.php?id=<?=$aVar['B_ID_AZIENDA']?>&xsn=<?=XSN?>'>
      	<?=$aVar['B_RAG_SOC']?></a></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Sede</td>
      <td class="txt1" align="left" colspan='17'><?=$aVar['B_SEDE']?></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Cod.sett.</td>
      <td class="txt1" align="left" colspan='3'><?=$aVar['B_COD_SETT']?></td>
      <td class="txt1" align="left" colspan='14'><?=$aVar['B_DESCR_SETT']?></td>
   </tr>
   <tr>
      <td class="lab1" colspan='3' align='left'>Modello Rating globale</td>
      <td class="txt1" colspan='17' align='left'>
      	<?echo getHtmlSelect('id_mod_rglob',$aSelMRG,'@REQ',array("onChange"=>"document.frm1.submit();"));?>
      </td>
   </tr>
   <tr>
      <td class="lab1" colspan='3' align='left'>Settore calcolo</td>
      <td class="txt1" colspan='17' align='left'>
      	<?echo getHtmlSelect('cod_sett',$aSett,'@REQ',array("onChange"=>"document.frm1.submit();"));?>
      </td>
   </tr>
	<tr>
		<td class='tit1'align='center' colspan='20'><b>INDICI DI BILANCIO</b></td>
	<tr>
<?
if ($acMRG) {
	$aGeDati = getGerarcDefDati($aDefDati,$acMRG['tipo_tab_indici']);
	//dbg_("BilRic gerarchia ".print_r($aGeDati,1));
	$sCls = 'txt1';
	foreach($aGeDati as $codX => $aX) {
		echoDato($codX,$aX,$aVar,0);
	}
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td> 
   </tr>
</table>
<?
echo "</form>\n";

codaHtml();
exit();


function echoDato($codX, $aX, &$aVar, $liv) {
	global $dataRif, $dataRifPrec, $objSess, $objDB, $sCls;
	$aCSez = array(''=>'-- non sezione --');
	$aCSez['spatt'] = 'Stato patrimoniale - Attivo';
	$aCSez['sppas'] = 'Stato patrimoniale - Passivo';
	$aCSez['cecon'] = 'Conto economico';
	$aCSez['altro'] = 'Altri dati';
	$aCSez['calco'] = 'Dati calcolati';
	
	if ($aX['attr']['cod_sez']!='') {
		$sCls = ($sCls=='txt1')?'txt2':'txt1';
		echo "<tr><td class='$sCls'align='center' colspan='12'><b>Indici di bilancio</b></td>\n"
			."</td><td class='$sCls' align='center' colspan='2'><i>".YMD2DMY($dataRif)."</i></td>"
			."<td class='$sCls' align='center' colspan='2'>media</td>\n"
			."<td class='$sCls' align='center' colspan='4'>valutazione</td>\n<tr>\n";
	} else {
		$codV = 'Q_'.$codX;
		switch ($aX['attr']['tipo_valore']) {
			case 'label':
				$numX = '';
				$valX = '';
				break;
			case 'euro':
			case 'num':
				$numX = (isset($aVar[$codV])) ? $aVar[$codV] : 0;
				$valX = number_format($numX,2,',','.');
				break;
		}
		$sSql = "SELECT p.* FROM rgl_mod_rglob mg "
			." INNER JOIN rgl_mod_rbil_par_ind p ON p.id_mod_rbil = mg.id_mod_rbil"
			."    AND cod_sett = '".reqVarAS('cod_sett')."' "
			."    AND cod_dato = '".addslashes($codX)."'"
			." WHERE mg.id_mod_rglob = ".reqVarInt('id_mod_rglob');
		$objDB->query($sSql,'selParInd');
		$sMedia = '&nbsp;';
		$sValutaz = '&nbsp;';
		$sStyle = '';
		if ($acPI = $objDB->fetch('selParInd')) {
			$sMedia = number_format($acPI['media_rif'],2,',','.');
			if ($numX >= $acPI['soglia100']) {
				$sValutaz = 'molto positivo';
				$sStyle = 'color:green;';
			} elseif ($numX >= $acPI['soglia75']) {
				$sValutaz = 'positivo';
				$sStyle = 'color:green;';
			} elseif ($numX >= $acPI['soglia50']) {
				$sValutaz = 'in media';
				$sStyle = 'color:#FFDD00;';
			} elseif ($numX >= $acPI['soglia25']) {
				$sValutaz = 'negativo';
				$sStyle = 'color:red;';
			} else {
				$sValutaz = 'molto negativo';
				$sStyle = 'color:red;';
			}
		}
		$sCls = ($sCls=='txt1')?'txt2':'txt1';
		echo "<tr><td class='$sCls' colspan='12'>".str_repeat("&nbsp;",$liv*8)
				."<a href='calcDato.php?cd=$codX&bil1=".reqVarInt('id')."&bil2=".reqVarInt('idp')
				."&xsn=".XSN."' class='txt1'>$codX</a> - ".$aX['attr']['descr']
				."</td><td class='$sCls' align='right' colspan='2'>$valX</td>\n"
				."<td class='$sCls' align='right' colspan='2'>$sMedia&nbsp;</td>\n"
				."<td class='$sCls' align='center' colspan='4' style='$sStyle'>$sValutaz</td>\n<tr>\n";
	}
	foreach($aX['figli'] as $codY => $aY)  {
		echoDato($codY, $aY, $aVar, $liv+1);
	}
}

